//
//  WBMainViewController.swift
//  小荣荣新浪微博
//
//  Created by zzr on 2018/4/19.
//  Copyright © 2018年 zzr. All rights reserved.
//

import UIKit

class WBMainViewController: UITabBarController {

    //MARK : - 私有控件
    //TabBar中间的加号按钮
    private lazy var composeButton: UIButton = {
        let btn = UIButton()
        //btn.frame = CGRect(x: 0, y: 0, width: <#T##CGFloat#>, height: <#T##CGFloat#>)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), for: .normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), for: .normal)
        btn.sizeToFit()
        
        //计算按钮的宽度
        let count = CGFloat(self.childViewControllers.count)
        //将向内缩进的宽度减少，能够让按钮的宽度变大，盖住容错点，防止穿帮
        let width = self.tabBar.bounds.width / count - 1
        btn.frame = tabBar.bounds.insetBy(dx: 2 * width, dy: 0)
        btn.addTarget(self, action: #selector(composeStatus), for: .touchUpInside)
        return btn
    }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupChildControllers()
        setupComposeButton()
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }

    //MARK: - 监听方法
    @objc fileprivate func composeStatus() {
        print("写微博")
        
        let vc = UIViewController()
        let nav = UINavigationController(rootViewController: vc)
        
        vc.view.backgroundColor = UIColor.randomColor()
        present(nav, animated: true, completion: nil)
    }
}


extension WBMainViewController {
    
    fileprivate func setupComposeButton() {
        tabBar.addSubview(composeButton)
        
    }
    
    //设置所有子控制器
    fileprivate func setupChildControllers() {
        
        
        
        let array:[[String:Any]] = [
            ["clsName":"WBHomeViewController", "title":"首页", "imageName":"home",
             "visitorInfo":["imageName":"", "message":"关注一些人"]
             ],
            ["clsName":"WBMessageViewController", "title":"消息", "imageName":"message_center",
             "visitorInfo":["imageName":"visitordiscover_image_message", "message":"登陆后，别人评论的信息都在这里"]
            ],
            ["clsName":"UiViewController"],
            ["clsName":"WBDiscoveryViewController", "title":"发现", "imageName":"discover",
             "visitorInfo":["imageName":"visitordiscover_image_message", "message":"登录后，发现最新潮流信息"]
            ],
            ["clsName":"WBProfileViewController", "title":"我", "imageName":"profile",
             "visitorInfo":["imageName":"visitordiscover_image_profile", "message":"登录后，你的个人信息"]
            ]
        ]
        //测试数据格式是否正确，转换成plist数据更佳直观
       // (array as NSArray).write(toFile: "/Users/zzr/Desktop/demo.plist", atomically: true)
        
        var arrayM: [UIViewController] = []
        for dict in array {
            arrayM.append(controller(dict: dict))
        }
        
        self.viewControllers = arrayM
        //设置tabbar的颜色
        UITabBar.appearance().tintColor = UIColor.orange
    }
    
    //创建一个子控制器
    fileprivate func controller(dict: [String : Any]) -> UIViewController {
        
        //1.取得字典内容
        guard let clsName = dict["clsName"] as? String,
            let title = dict["title"] as? String,
            let imageName = dict["imageName"] as? String,
            let cls = NSClassFromString(Bundle.main.namespace + "." + clsName) as? WBBaseViewController.Type,
            let visitorDict = dict["visitorInfo"] as? [String:String]
        else {
                let demo = WBDemoViewController()
                return demo
        }
        //2.创建视图控制器
        let vc = cls.init()
        vc.title = title
        
        vc.visitorInfoDict = visitorDict
        
        //3.设置图像
        vc.tabBarItem.image = UIImage(named: "tabbar_" + imageName)
        vc.tabBarItem.selectedImage = UIImage(named: "tabbar_" + imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        
        
        //vc.tabBarItem.setTitleTextAttributes([], for: <#T##UIControlState#>)
        //vc.tabBarItem.setTitleTextAttributes([NSAttributedStringKey.Ty:UIFont.systemFont(ofSize: 12)], for: .highlighted)
        
        //实例化导航控制器的时候，会调用push方法将rootVC压栈
        let nav = WBNavigationViewController(rootViewController: vc)
        
        return nav
    }
}
